<?php

namespace app\index\controller;

use app\index\model\User;
use app\BaseController;
use app\index\model\Article;
use app\index\model\Msg;
use app\index\model\Sign;
use app\index\model\Tap;
use app\service\Score;
use think\facade\Db;

class Index extends BaseController
{
    protected $middleware = ['isLogin' => ['except' => ['index']]];
    public function index()
    {
        $this->logs('论坛首页');
        $tap = input('tag');
        $art = new Article();
        $data = $art->list(['limit' => 5, 'status' => 1, 'tap' => $tap]);
        $page = $data['list']->render();
        $tapList = (new Tap())->list();
        $data['title'] = Tap::hasTag($tap) != 0 ?  Tap::hasTag($tap) : '最新文章';
        if(!empty($tap)){
            $hasTag = Tap::findOrEmpty($tap)->isEmpty();
            if(!$hasTag){
                Db::name('tap')->where('id', $tap)->inc('heat')->update();
            }
        }
        $hotArticle = $art::where('heat', '>', 100)->where('status',1)->order('heat desc')->limit(5)->select();
        $newArt = $art::order('id desc')->where('status',1)->limit(5)->select();
        $sign = [];
        if($this->user)
        {
            $sign = Sign::get();
            $sign['isSign'] = Sign::isSign();
            $sign['signDay'] = $sign->day;
            $sign['signScore'] = Sign::rule($sign->day);
        }
        $sign['newSignLis'] = Sign::newSignLis();
        $sign['todayFastLis'] = Sign::todayFastLis();
        $sign['totalLis'] = Sign::totalLis();
        return view('index', ['data' => $data, 'sign' => $sign , 'tapList' => $tapList, 'hotArticle' => $hotArticle, 'page' => $page ,'newArt' => $newArt]);
    }
    public function mine()
    {
        $userData = (new User())->profile($this->user['user_id']);
        return view('mine',['userData' => $userData]);
    }
    public function sign()
    {
        $res = Sign::do();
        $uid = $this->user['user_id'];
        if($res != 1) return error($res);
        $score = Sign::rule(Sign::get()->day);
        Score::add($uid ,$score);
        Msg::send(0,$uid,['content' => "签到成功，获得 $score 积分"]);
        return success(); 
    }
  
}
